
package com.craterhill.java.ems.web;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.AutoPopulatingList;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.craterhill.java.ems.common.beans.GradingRules;
import com.craterhill.java.ems.common.beans.MarkingPeriods;
import com.craterhill.java.ems.common.beans.Session;
import com.craterhill.java.ems.service.SchoolService;
import com.craterhill.java.ems.service.SessionService;

@Controller
public class SessionPeriodGradeManagement {

	private final SchoolService schoolService;
	private final SessionService sessionService;

	@Autowired
	public SessionPeriodGradeManagement(final SchoolService schoolService, final SessionService sessionService) {
		this.schoolService = schoolService;
		this.sessionService = sessionService;
	}

	@RequestMapping(value = "/setupSessionPeriodGradeManagementForm.ems", method = RequestMethod.GET)
	public String setupForm(final @RequestParam(required = false, defaultValue = "0") int schoolId,
			final @RequestParam(required = false, defaultValue = "0") int sessionId, final ModelMap modelMap) {
		modelMap.addAttribute("schoolList", schoolService.getSchools());
		if(sessionId != 0){
			Session session = sessionService.getSession(sessionId);
			if(session.getMarkingPeriods() == null || session.getMarkingPeriods().isEmpty()){
				session.getMarkingPeriods().add(new MarkingPeriods());
			}
			modelMap.addAttribute("session", sessionService.getSession(sessionId));
		}
		return "manage_session_period_grade";
	}

	@RequestMapping(value = "/saveSchoolSessionDetails.ems", method = RequestMethod.POST)
	public String saveSession(final @ModelAttribute Session session) {
		if(session.getSessionId() == 0){
			sessionService.saveSession(session);
		} else{
			sessionService.updateSession(session);
		}
		return "redirect:setupSessionPeriodGradeManagementForm.ems";
	}

	@RequestMapping(value = "/deleteSession.ems", method = RequestMethod.GET)
	public String deletePosition(final @RequestParam int sessionId) {
		sessionService.removeSession(sessionId);
		return "redirect:setupSessionPeriodGradeManagementForm.ems";
	}

	@RequestMapping(value = "/getAjaxSessionList.ems", method = RequestMethod.GET)
	public void getAjaxSessionList(final @RequestParam int schoolId, final HttpServletResponse httpServletResponse) {
		List<Session> sessionList = sessionService.getSessions(schoolId);

		JSONArray sessionListJson = new JSONArray();
		JSONObject sessionJsonObj = null;
		for(Session session : sessionList){
			sessionJsonObj = new JSONObject();
			sessionJsonObj.put("sessionId", session.getSessionId());
			sessionJsonObj.put("sessionName", session.getSessionName());
			sessionListJson.add(sessionJsonObj);
		}

		try{
			httpServletResponse.getWriter().write(sessionListJson.toString());
		} catch(IOException e){
			e.printStackTrace();
		}
	}

	@ModelAttribute
	public Session getSession() {
		Session session = new Session();
		AutoPopulatingList<MarkingPeriods> markingPeriodList = new AutoPopulatingList<MarkingPeriods>(MarkingPeriods.class);
		markingPeriodList.add(new MarkingPeriods());
		session.setMarkingPeriods(markingPeriodList);
		AutoPopulatingList<GradingRules> gradingRules = new AutoPopulatingList<GradingRules>(GradingRules.class);
		gradingRules.add(new GradingRules());
		session.setGradingRules(gradingRules);
		return session;
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}